package com.aop.bean;

import com.aop.annotation.MyAnnotation;
import org.springframework.aop.framework.AopContext;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * @Author: tgy
 * @Date: 2020-08-06 11:47
 */
public class Student implements Person, ApplicationContextAware, BeanFactoryAware {


    private ApplicationContext applicationContext = null;

    @Override
    public void eat(String food) {

        System.out.println("bean eat" + food);
        /**
         * 1. 实现 ApplicationContextAware 接口，获取 ApplicationContext
         *
         * 2. 在 xml 配置文件中的 <aop:aspectj-autoproxy expose-proxy="true"></aop:aspectj-autoproxy>
         *     设置 expose-proxy="true"
         *      原理是 通过线程的threadlocal 暴露 proxy对象
         */
//        this.eat();
//        Person studentProxy = applicationContext.getBean(Person.class);
//
//        studentProxy.eat();
        Person currentProxy = (Person)AopContext.currentProxy();
        if (currentProxy != null){

            currentProxy.eat();
        }
    }

    @Override
    @MyAnnotation
    public void eat(Integer p, String food) {


        System.out.println("bean eat" + p + food);
    }

    @Override
    @MyAnnotation
    public void eat() {

        System.out.println("bean eat");
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

        this.applicationContext = applicationContext;
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {

    }
}
